package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Curriculum;
import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.query.CurriculumQueryObject;
import cn.wolfcode.crm.service.ICurriculumService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IGradeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("curriculum")
public class CurriculumController {
    @Autowired
    private ICurriculumService curriculumService;
    @Autowired
    private IGradeService gradeService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("view")
    public String query() {
        return "curriculum";
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CurriculumQueryObject qo){
        return curriculumService.getAll(qo);
    }


    @RequestMapping("export")
    public void export(HttpServletResponse resp) throws IOException {
        //设置下载响应头
        resp.setHeader("Content-Disposition","attachment;filename=curriculum.xls");
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = wb.createSheet();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        //创建单元格,设置标题
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("星期");
        row.createCell(2).setCellValue("班级");
        row.createCell(3).setCellValue("课程");
        row.createCell(4).setCellValue("班主任");
        row.createCell(5).setCellValue("任课教师");
        row.createCell(6).setCellValue("备注");
        //输出到浏览器
        wb.write(resp.getOutputStream());
    }


    @RequestMapping("import")
    @ResponseBody
    public Object importXls(MultipartFile file) throws IOException, ParseException {
        JSONResult jsonResult = new JSONResult();
        try {
            //获取excel文件
            HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
            //获取工作薄
            HSSFSheet sheet1 = wb.getSheet("sheet0");
            //获取总行数
            int lastRowNum = sheet1.getLastRowNum();
            System.out.println(lastRowNum);
            //循环所有行,第0行是标题,从第一行开始,一行一个对象
            for (int i = 1; i<= lastRowNum; i++){
                //获取每一行
                HSSFRow row = sheet1.getRow(i);
                Curriculum curriculum = new Curriculum();
                //获取每个单元格内容,设置到对象中
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                curriculum.setDate(row.getCell(0).getDateCellValue());
                curriculum.setWeek(row.getCell(1).getStringCellValue());
                curriculum.setGrade(gradeService.getByClassName(row.getCell(2).getStringCellValue()));
                curriculum.setCourse(row.getCell(3).getStringCellValue());
                curriculum.setCharge(employeeService.selectByUsername(row.getCell(4).getStringCellValue()));
                curriculum.setTeacher(employeeService.selectByUsername(row.getCell(5).getStringCellValue()));
                curriculum.setRemark(row.getCell(6).getStringCellValue());
                curriculumService.save(curriculum);
            }
        }catch (Exception e){
            jsonResult.remark("参数异常,上传失败");
        }
        return jsonResult;
    }
}
